Product
Socket Now Supports uv.lock Files
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
@rollup/plugin-virtual
Advanced tools
@rollup/plugin-virtual is a Rollup plugin that allows you to create virtual modules. These are modules that do not exist as files on disk but are instead created in memory. This can be useful for injecting code, creating mock modules for testing, or dynamically generating content during the build process.
Creating a virtual module
This feature allows you to create a virtual module named 'my-virtual-module' with the content 'export default "This is virtual!"'. This module can then be imported and used in your Rollup bundle.
const virtual = require('@rollup/plugin-virtual');
module.exports = {
input: 'src/main.js',
plugins: [
virtual({
'my-virtual-module': 'export default "This is virtual!"'
})
],
output: {
file: 'bundle.js',
format: 'es'
}
};
Mocking modules for testing
This feature allows you to mock the 'fs' module with a virtual module that provides a custom implementation of the 'readFile' function. This can be useful for testing purposes.
const virtual = require('@rollup/plugin-virtual');
module.exports = {
input: 'src/main.js',
plugins: [
virtual({
'fs': 'export default { readFile: () => "mocked content" }'
})
],
output: {
file: 'bundle.js',
format: 'es'
}
};
Dynamically generating content
This feature allows you to create a virtual module with content that is dynamically generated at build time. In this example, the module exports a string with the current date and time.
const virtual = require('@rollup/plugin-virtual');
module.exports = {
input: 'src/main.js',
plugins: [
virtual({
'dynamic-module': `export default "Generated at ${new Date().toISOString()}"`
})
],
output: {
file: 'bundle.js',
format: 'es'
}
};
rollup-plugin-inject allows you to inject variables or modules into your bundle. While it does not create virtual modules, it can be used to achieve similar results by injecting code into existing modules.
rollup-plugin-replace allows you to replace strings in your code during the build process. It can be used to inject dynamic content or mock modules, similar to @rollup/plugin-virtual, but it operates by replacing existing code rather than creating new virtual modules.
rollup-plugin-alias allows you to create aliases for module paths. While it does not create virtual modules, it can be used to redirect module imports to different files or modules, which can be useful for testing or injecting custom implementations.
🍣 A Rollup plugin which loads virtual modules from memory.
This plugin requires an LTS Node version (v14.0.0+) and Rollup v1.20.0+.
Using npm:
npm install @rollup/plugin-virtual --save-dev
Note. Use this plugin before any others such as node-resolve or commonjs, so they do not alter the output.
Suppose an entry file containing the snippet below exists at src/entry.js
, and attempts to load batman
and src/robin.js
from memory:
// src/entry.js
import batman from 'batman';
import robin from './robin.js';
console.log(batman, robin);
Create a rollup.config.js
configuration file and import the plugin:
import virtual from '@rollup/plugin-virtual';
export default {
input: 'src/entry.js',
// ...
plugins: [
virtual({
batman: `export default 'na na na na na'`,
'src/robin.js': `export default 'batmannnnn'`
})
]
};
Then call rollup
either via the CLI or the API.
This plugin has no formal options. The lone parameter for this plugin is an Object
containing properties that correspond to a String
containing the virtual module's code.
It's possible to use the plugin to specify an entry point for a bundle. To do so, implement a pattern simple to what is shown below:
import virtual from '@rollup/plugin-virtual';
export default {
input: 'entry',
// ...
plugins: [
virtual({
entry: `
import batman from 'batcave';
console.log(batman);
`
})
]
};
FAQs
Load virtual modules from memory
The npm package @rollup/plugin-virtual receives a total of 123,472 weekly downloads. As such, @rollup/plugin-virtual popularity was classified as popular.
We found that @rollup/plugin-virtual demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.